package com.particle.data.app.company.executor.warehouse;

import com.particle.common.app.executor.AbstractBaseExecutor;
import com.particle.data.app.company.executor.DataCompanyOpenCourtAnnouncementContentCreateCommandExecutor;
import com.particle.data.app.company.executor.DataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor;
import com.particle.data.app.company.executor.representation.exwarehouse.DataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor;
import com.particle.data.client.company.dto.command.DataCompanyOpenCourtAnnouncementContentCreateCommand;
import com.particle.data.client.company.dto.command.DataCompanyOpenCourtAnnouncementContentUpdateCommand;
import com.particle.data.client.company.dto.command.representation.exwarehouse.DataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand;
import com.particle.data.client.company.dto.command.warehouse.DataCompanyOpenCourtAnnouncementContentWarehouseCommand;
import com.particle.data.client.company.dto.data.exwarehouse.DataCompanyOpenCourtAnnouncementContentExWarehouseVO;
import com.particle.data.infrastructure.company.service.IDataCompanyOpenCourtAnnouncementContentService;
import com.particle.global.dto.response.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 企业开庭公告内容入库 指令执行器
 * </p>
 *
 * @author yw
 * @since 2025-04-07 11:14:10
 */
@Component
@Validated
public class DataCompanyOpenCourtAnnouncementContentWarehouseCommandExecutor extends AbstractBaseExecutor {

	private DataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor;
	private DataCompanyOpenCourtAnnouncementContentCreateCommandExecutor dataCompanyOpenCourtAnnouncementContentCreateCommandExecutor;
	private IDataCompanyOpenCourtAnnouncementContentService iDataCompanyOpenCourtAnnouncementContentService;
	private DataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor dataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor;


	/**
	 * 企业开庭公告内容入库
	 * @param dataCompanyOpenCourtAnnouncementContentWarehouseCommand
	 * @return
	 */
	public SingleResponse<DataCompanyOpenCourtAnnouncementContentExWarehouseVO> warehouse(DataCompanyOpenCourtAnnouncementContentWarehouseCommand dataCompanyOpenCourtAnnouncementContentWarehouseCommand) {
		DataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand dataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand = DataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand.create(dataCompanyOpenCourtAnnouncementContentWarehouseCommand.getCompanyOpenCourtAnnouncementId());
		SingleResponse<DataCompanyOpenCourtAnnouncementContentExWarehouseVO> dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse = dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor.exWarehouse(dataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand);
		DataCompanyOpenCourtAnnouncementContentExWarehouseVO dataCompanyOpenCourtAnnouncementContentExWarehouseVO = dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse.getData();
		// 不存在，添加
        if (dataCompanyOpenCourtAnnouncementContentExWarehouseVO == null) {
			DataCompanyOpenCourtAnnouncementContentCreateCommand dataCompanyOpenCourtAnnouncementContentCreateCommand = DataCompanyOpenCourtAnnouncementContentCreateCommand.createByWarehouseCommand(dataCompanyOpenCourtAnnouncementContentWarehouseCommand);
			dataCompanyOpenCourtAnnouncementContentCreateCommandExecutor.execute(dataCompanyOpenCourtAnnouncementContentCreateCommand);
			// 新增后重新查询，返回最新数据
			dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse = dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor.exWarehouse(dataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand);
			return dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse;
		}else {
			// 	存在，尝试入库
			// 仅更新有变化的字段，将相同的字段设置为null不更新
			dataCompanyOpenCourtAnnouncementContentWarehouseCommand.compareAndSetNullWhenEquals(dataCompanyOpenCourtAnnouncementContentExWarehouseVO);

			// 判断是否所有字段都为空，所有字段都没有变化，不需要更新
			if (dataCompanyOpenCourtAnnouncementContentWarehouseCommand.allFieldEmpty()) {
				// 更新最后处理时间
				iDataCompanyOpenCourtAnnouncementContentService.updateLatestHandleAt(dataCompanyOpenCourtAnnouncementContentExWarehouseVO.getId());
				// 如果没有需要更新的字段，则直接返回
				return dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse;
			} else {
				// 更新处理
				DataCompanyOpenCourtAnnouncementContentUpdateCommand dataCompanyOpenCourtAnnouncementContentUpdateCommand = DataCompanyOpenCourtAnnouncementContentUpdateCommand.createByWarehouseCommand(
						dataCompanyOpenCourtAnnouncementContentExWarehouseVO.getId(),
						dataCompanyOpenCourtAnnouncementContentExWarehouseVO.getVersion(),
						dataCompanyOpenCourtAnnouncementContentWarehouseCommand
				);
				dataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor.execute(dataCompanyOpenCourtAnnouncementContentUpdateCommand);
			}

			// 更新完成后，新增的情况已经在新增逻辑里面直接返回了，查询返回
			dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse = dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor.exWarehouse(dataCompanyOpenCourtAnnouncementContentExWarehouseQueryCommand);
			return dataCompanyOpenCourtAnnouncementContentExWarehouseVOSingleResponse;
		}
	}

	@Autowired
	public void setDataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor(DataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor) {
		this.dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor = dataCompanyOpenCourtAnnouncementContentExWarehouseCommandExecutor;
	}
	@Autowired
	public void setDataCompanyOpenCourtAnnouncementContentCreateCommandExecutor(DataCompanyOpenCourtAnnouncementContentCreateCommandExecutor dataCompanyOpenCourtAnnouncementContentCreateCommandExecutor) {
		this.dataCompanyOpenCourtAnnouncementContentCreateCommandExecutor = dataCompanyOpenCourtAnnouncementContentCreateCommandExecutor;
	}
	@Autowired
	public void setiDataCompanyOpenCourtAnnouncementContentService(IDataCompanyOpenCourtAnnouncementContentService iDataCompanyOpenCourtAnnouncementContentService) {
		this.iDataCompanyOpenCourtAnnouncementContentService = iDataCompanyOpenCourtAnnouncementContentService;
	}
	@Autowired
	public void setDataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor(DataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor dataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor) {
		this.dataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor = dataCompanyOpenCourtAnnouncementContentUpdateCommandExecutor;
	}
}
